package com.zzg.mybatis.generator.plugins;

import org.mybatis.generator.api.*;
import org.mybatis.generator.api.dom.java.*;

import com.zzg.mybatis.generator.util.StringUtil;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * mapper接口参数名自定义
 */
public class CustomizeDAOInterfacePlugin extends PluginAdapter{

	private static final FullyQualifiedJavaType LIST_TYPE = FullyQualifiedJavaType.getNewListInstance();

	private void interceptSelectModelParam(Method method,Interface interfaze,IntrospectedTable introspectedTable){
		interfaze.getImportedTypes().remove(new FullyQualifiedJavaType("java.math.BigDecimal"));
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        importedTypes.add(LIST_TYPE);
        interfaze.addImportedTypes(importedTypes);
        
		String remarks = ""; //接口备注
		String newMethodName = ""; //新的方法名
		String oldMethodName = method.getName(); //旧的方法名
		FullyQualifiedTable fullyTableName = introspectedTable.getFullyQualifiedTable();
		String domainObjectName = fullyTableName.getDomainObjectName();
		int secondIndex = domainObjectName.indexOf("_",domainObjectName.indexOf("_")+1);
		String tempStr = StringUtil.customClassName(domainObjectName.substring(secondIndex+1).replace("Po",""),"");
		if(oldMethodName.contains("select")){
			remarks = "查询"+introspectedTable.getRemarks();
			newMethodName = "query" + tempStr;
		}
		method.setName(newMethodName);
		method.setVisibility(JavaVisibility.PUBLIC);
		method.getParameters().clear();
		method.addParameter(new Parameter(new FullyQualifiedJavaType(domainObjectName),"pageInfo"));
		method.setReturnType(new FullyQualifiedJavaType("List<"+fullyTableName.getDomainObjectName()+">"));
		method.addJavaDocLine("/**");
		method.addJavaDocLine(" * "+ remarks);
		method.addJavaDocLine(" * @param "+ "pageInfo");
		method.addJavaDocLine(" * @return ");
		method.addJavaDocLine(" */");
	}

	private void interceptModelParam(Method method,Interface interfaze,IntrospectedTable introspectedTable){
		interfaze.getImportedTypes().remove(new FullyQualifiedJavaType("java.math.BigDecimal"));
		String remarks = ""; //接口备注
		String newMethodName = ""; //新的方法名
		String oldMethodName = method.getName(); //旧的方法名
		FullyQualifiedTable fullyTableName = introspectedTable.getFullyQualifiedTable();
		String domainObjectName = fullyTableName.getDomainObjectName();
		int secondIndex = domainObjectName.indexOf("_",domainObjectName.indexOf("_")+1);
		String tempStr = StringUtil.customClassName(domainObjectName.substring(secondIndex+1).replace("Po",""),"");
		method.getParameters().clear();
		if(oldMethodName.contains("insert")){
			remarks = "新增"+introspectedTable.getRemarks();
			newMethodName = "insert" + tempStr;
		}else if(oldMethodName.contains("update")){
			remarks = "修改"+introspectedTable.getRemarks();
			newMethodName = "update" + tempStr;
		}else if(oldMethodName.contains("delete")){
			remarks = "删除"+introspectedTable.getRemarks();
			newMethodName = "delete" + tempStr;
		}
		method.setName(newMethodName);
		method.setVisibility(JavaVisibility.PUBLIC);
		method.addParameter(new Parameter(new FullyQualifiedJavaType(domainObjectName),"pageInfo"));
		method.addJavaDocLine("/**");
		method.addJavaDocLine(" * "+ remarks);
		method.addJavaDocLine(" * @param "+ (oldMethodName.contains("delete")?"id":"pageInfo"));
		method.addJavaDocLine(" * @return ");
		method.addJavaDocLine(" */");
	}

	@Override
	public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles() {
		return null;
	}

	@Override
	public boolean clientGenerated(Interface interfaze,TopLevelClass topLevelClass,IntrospectedTable introspectedTable) {
		return true;
	}

	@Override
	public boolean validate(List<String> warnings) {
		return true;
	}

	@Override
	public boolean clientSelectByPrimaryKeyMethodGenerated(Method method,Interface interfaze,IntrospectedTable introspectedTable) {
		interceptSelectModelParam(method,interfaze,introspectedTable);
		return true;
	}
	@Override
	public boolean clientInsertSelectiveMethodGenerated(Method method,Interface interfaze,IntrospectedTable introspectedTable) {
		interceptModelParam(method,interfaze,introspectedTable);
		return true;
	}
	@Override
	public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method,Interface interfaze,IntrospectedTable introspectedTable) {
		interceptModelParam(method,interfaze,introspectedTable);
		return true;
	}
	@Override
	public boolean clientDeleteByPrimaryKeyMethodGenerated(Method method,Interface interfaze,IntrospectedTable introspectedTable) {
		interceptModelParam(method,interfaze,introspectedTable);
		return true;
	}


	@Override
	public boolean clientInsertMethodGenerated(Method method,Interface interfaze,IntrospectedTable introspectedTable){
		return false;
	}

	@Override
	public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method,TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		return false;
	}

	@Override
	public boolean clientUpdateByPrimaryKeyWithBLOBsMethodGenerated(Method method,Interface interfaze,IntrospectedTable introspectedTable) {
		return false;
	}

	@Override
	public boolean clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated(Method method,Interface interfaze,IntrospectedTable introspectedTable) {
		return false;
	}
}